<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Api\LoanController;
use App\Models\CityTicket;
use App\Models\LoanApplication;
use App\Models\LoanTypeList;

/**
 * 内部函数
 */
class Internal
{

    public static function RechargeLogic($rechargeAmount){
        $rechargeAmount = intval($rechargeAmount);
        if ($rechargeAmount >= 5000) {
            return $rechargeAmount + 1000;
        } elseif ($rechargeAmount >= 2000) {
            return $rechargeAmount + 400;
        } elseif ($rechargeAmount >= 1000) {
            return $rechargeAmount + 100;
        }
        return $rechargeAmount;
    }


    public static function getStatusName($status)
    {
        $statusArray = [
            0 =>'未更新',
            1 =>'待跟进',
            2 =>'已通话',
            3 =>'已上门',
            4 =>'已签约',
            5 =>'已放款',
            6 =>'已拒绝',
            7 =>'恶意/骚扰',
            8 =>'无效对话',
            9 =>'空错号',
            10 =>'回访-电话3次未接通',
            11 =>'非本人',
            12 =>'无意向',
            13 =>'异地',
            14 =>'资质不符',
            15 =>'退款中',
            16 =>'退款成功',
            17 =>'退款失败'
        ];
        return $statusArray[$status];
    }

    public static function getNeedTicket($type, $city,$loan_id)
    {

        if($type == '综合'){
            $loan_data = LoanApplication::where('id',$loan_id)->first()->toArray();
            $ticket = [];
            $default_ticket = [];
            $type = ['普通车'];
            if($loan_data['has_house'] == '有'){
                $type[] = '普通房';
            }
            if($loan_data['yingyezhizhao'] == '有'){
                $type[] = '普通企业主';
            }
            if($loan_data['has_gongjijin'] == '有'){
                $type[] = '普通公积金';
            }
            $CityTicket = CityTicket::whereIn('type', $type)->get()->toArray();
            foreach ($CityTicket as $value) {
                if(strpos($city,$value['city']) !== false){
                    $ticket[] = $value['ticket'];
                }
                if($value['city'] == '默认'){
                    $default_ticket[] = $value['ticket'];
                }
            }
           if(count($ticket)>0){
               $ticket = max($ticket);
           }else{
               $ticket = max($default_ticket);
           }
        }else{
            $ticket =0;
            $CityTicket = CityTicket::where('type', $type)->get()->toArray();
            foreach ($CityTicket as $value) {
                if(strpos($city,$value['city']) !== false){
                    $ticket = $value['ticket'];
                }
            }
            if(!$ticket){
                $CityTicket = CityTicket::where(['type'=>$type,'city'=>'默认'])->pluck('ticket');
                $ticket = $CityTicket?$CityTicket->first():100;
            }
        }


        return $ticket;
    }


    public static function hiddenInfoCity($city)
    {
        $city_list = [];
        if(strpos($city,' ') !== false){
            $city = str_replace('-','',$city);
            $city_list = explode(' ',$city);
        }
        if(strpos($city,'-') !== false){
            $city_list = explode('-',$city);
        }
        $str  = '';
        foreach ($city_list as $k=>$value) {
            if($k<=1){
                $str .= $value.' ';
            }
        }
        return strlen($str)>0?$str:$city;
    }

    public static function getDataPQ($data)
    {
        if(!$data || $data =='无' || $data =='-'){
            return '待确认';
        }
        return $data;
    }


    public static function getCategoryType($type)
    {

        $loan_type = LoanTypeList::where('loan_ename',$type)->pluck('loan_type')->first();

        return $loan_type;
    }



}
